﻿#region References
using System;
using System.Net;
using System.Net.Mail;
using System.ComponentModel;

using Autumoon.IPInformer.Fundamental;
#endregion

namespace Autumoon.IPInformer.ModuleLayer
{
    public static class InformManager
    {

        #region Fields and Properties
        #endregion

        #region Constructors
        #endregion

        #region Public Methods
        public static InformOptionProperty GetInformOption()
        {
            // Notice: Implement this function.

            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinaryIP"></param>
        /// <param name="currentIP"></param>
        /// <param name="informOptions"></param>
        /// <returns>Informed, if the two IP properties are not the same, then reture true.</returns>
        public static bool Inform(IPProperty ordinaryIP, IPProperty currentIP, InformOptionProperty informOptions)
        {
            if (currentIP == null)
            {
                return false;
            }

            if (ordinaryIP == null || ordinaryIP.InternalIP != currentIP.InternalIP || ordinaryIP.PublicIP != currentIP.PublicIP)
            {
                Inform(currentIP, informOptions);

                return true;
            }

            return false;
        }
        #endregion

        #region Internal Methods
        #endregion

        #region Protected Methods
        #endregion

        #region Private Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentIP"></param>
        /// <param name="informOptions"></param>
        private static void Inform(IPProperty currentIP, InformOptionProperty informOptions)
        {
            if (informOptions == null)
            {
                return;
            }

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(informOptions.SenderOutbox);
            mailMessage.To.Add(new MailAddress(informOptions.ReceiverInbox));
            mailMessage.Subject = "IP Information";
            mailMessage.Body = String.Format("Your current internal IP is: {0};\nYour current public IP is: {1};\n\nUpdate Time:{2};\n\n\nSend by Autumoon IP Informer.", currentIP.InternalIP, currentIP.PublicIP, currentIP.UpdateDateTime);

            SmtpClient smtpClient = new SmtpClient(informOptions.SmtpServerAddress);
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential(informOptions.UserName, Utility.Decrypt(informOptions.EncryptedPassword));
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);

            smtpClient.SendAsync(mailMessage, null);
        }

        public static void Save(InformOptionProperty informOptionProperty)
        {
        }
        #endregion

        #region Delegates and Events
        private static void smtpClient_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                throw new Exception(e.Error.Message, e.Error);
            }
        }
        #endregion
    }
}